/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement.Components;

import org.virtualbox_3_2.StorageBus;
import org.virtualbox_3_2.StorageControllerType;

/**
 * This class represents the settings of a storage controller in the virtual machine.
 *
 * @author Angel Sanadinov
 */
public class ControllerSettings extends MachineManagementComponent
{
    private String controllerName;                //name of the controller
    private StorageBus connectionType;            //controller connection type
    private StorageControllerType controllerType; //controller type
    private boolean useHostIOCache;               //denotes whether the host I/O cache should be used

    /**
     * Constructs a controller settings object with the specified data.
     *
     * @param controllerName the name of the controller
     * @param connectionType the type of the controller connection
     * @param controllerType the type of the controller
     * @param useHostIOCache denotes whether the host I/O cache will be used
     */
    public ControllerSettings(String controllerName, StorageBus connectionType, 
                              StorageControllerType controllerType, boolean useHostIOCache)
    {
        this.controllerName = controllerName;
        this.connectionType = connectionType;
        this.controllerType = controllerType;
        this.useHostIOCache = useHostIOCache;
    }

    /**
     * Retrieves the storage controller name.
     *
     * @return the name of the storage controller
     */
    public String getControllerName()
    {
        return controllerName;
    }

    /**
     * Retrieves the connection type of the controller.
     *
     * @return the connection type
     */
    public StorageBus getConnectionType()
    {
        return connectionType;
    }

    /**
     * Retrieves the type of the controller.
     *
     * @return the controller type
     */
    public StorageControllerType getControllerType()
    {
        return controllerType;
    }

    /**
     * Retrieves the state of the host I/O cache usage for the controller.
     *
     * @return <code>true</code> if host I/O cache should be used or <code>false</code>
     *         if not
     */
    public boolean getUseHostIOCache()
    {
        return useHostIOCache;
    }

    /**
     * Checks if the port/slot pair is valid for this controller.
     *
     * @param port the controller port
     * @param slot the controller slot
     * @return <code>true</code> if the pair is valid or <code>false</code> otherwise
     */
    public boolean isPortSlotPairValidForController(int port, int slot)
    {
        boolean result = false;

        switch(connectionType)
        {
            case FLOPPY:
            {
                if(port == 0 && (slot == 0 || slot == 1))
                    result = true;
                else
                    ;
            } break;

            case IDE:
            {
                if((port == 0 || port == 1) && (slot == 0 || slot == 1))
                    result = true;
                else
                    ;
            } break;

            case SAS:
            {
                if(slot == 0 && port >= 0 && port <= 7)
                    result = true;
                else
                    ;
            } break;

            case SATA:
            {
                if(slot == 0 && port >= 0 && port <= 29)
                    result = true;
                else
                    ;
            } break;

            case SCSI:
            {
                if(slot == 0 && port >= 0 && port <= 15)
                    result = true;
                else
                    ;
            } break;

            default: result = false;
        }

        return result;
    }

    /**
     * Checks if the controller type is valid.
     *
     * @return <code>true</code> if the controller type is valid or <code>false</code>
     *         otherwise
     */
    private boolean isControllerTypeValid()
    {
        boolean result = false;

        switch(connectionType)
        {
            case FLOPPY:
            {
                if(controllerType == StorageControllerType.I_82078)
                    result = true;
                else
                    ;
            } break;

            case IDE:
            {
                if(controllerType == StorageControllerType.PIIX_3
                        || controllerType == StorageControllerType.PIIX_4
                        || controllerType == StorageControllerType.ICH_6)
                    result = true;
                else
                    ;
            } break;

            case SAS:
            {
                if(controllerType == StorageControllerType.LSI_LOGIC_SAS)
                    result = true;
                else
                    ;
            } break;

            case SATA:
            {
                if(controllerType == StorageControllerType.INTEL_AHCI)
                    result = true;
                else
                    ;
            } break;

            case SCSI:
            {
                if(controllerType == StorageControllerType.LSI_LOGIC
                        || controllerType == StorageControllerType.BUS_LOGIC)
                    result = true;
                else
                    ;
            } break;

            default: result = false;
        }

        return result;
    }

    @Override
    public boolean isValid()
    {
        if(controllerName != null && connectionType != null && controllerType != null
                && isControllerTypeValid())
            return true;
        else
            return false;
    }
}
